{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit FunctionDescriptor;

interface

uses Classes, XmlHelper, Functions, Actions, Descriptor;

const
  FILE_FUNCTION_DESCRIPTOR = 'FunctionDescriptions.xml';

type
  TAHMFunctionDescriptor = class(TAHMDescriptor)
  private
    FFunctionList: TAHMFunctionList;
    FActionList: TAHMActionList;
  protected
    procedure ParseXML; override;
    procedure SaveAsXML(Lines: TStrings); override;
  public
    constructor Create; override;
    destructor Destroy; override;
    property FunctionList: TAHMFunctionList read FFunctionList;
    property ActionList: TAHMActionList read FActionList;
    procedure LoadDescriptor;
  end;

implementation

uses ApplicationManager, TagNames, XMLTags, ErrorCodes, SysUtils;

constructor TAHMFunctionDescriptor.Create;
begin
  inherited Create;

  FFileName := FILE_FUNCTION_DESCRIPTOR;
  FRootTagName := TAG_FUNCTION_DESCRIPTIONS;

  FFunctionList := TAHMFunctionList.Create;
  FActionList   := TAHMActionList.Create;
end;

destructor TAHMFunctionDescriptor.Destroy;
begin
  FFunctionList.FreeWithContents;
  FActionList.FreeWithContents;

  inherited Destroy;
end;

procedure TAHMFunctionDescriptor.LoadDescriptor;
begin
  // Clear any existing function & action definitions before we start
  FFunctionList.Clear;
  FActionList.Clear;

  // Parse XML to populate function definitions
  ParseXML(ApplicationManagerInstance.ConfigPath);
end;

procedure TAHMFunctionDescriptor.ParseXML;
var
  RootNode: TAHMXMLNode;
begin
  // What nodes do we have here?
  RootNode := FHelper.CurrentNode;
  FParseNode := nil;

  while FHelper.NextElement(RootNode, FParseNode) do
  begin
    if FHelper.CurrentElement = TAG_FUNCTIONS then
      FFunctionList.ParseFromXML(FHelper)
    else if FHelper.CurrentElement = TAG_ACTIONS then
      FActionList.ParseFromXML(FHelper)
    else
      LogParsingError(XML_PRS_FNC_FOUND_UNKNOWN_CONTENT_TAG);
  end;
end;

procedure TAHMFunctionDescriptor.SaveAsXML(Lines: TStrings);
begin
  // Write all interface descriptor lists to XML file
  FFunctionList.SaveAsXML(Lines);
  FActionList.SaveAsXML(Lines);
end;


end.
